package com.woniuxy.lrz.carlproject;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MybatisPlusGeneratorExecutor {
    public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/carldb";
    public static final String JDBC_USER = "root";
    public static final String JDBC_PASSWORD = "root";
    public static final String AUTHOR = "lrz";

    public static final String PARENT_PACKAGE = "com.woniuxy.lrz";
    public static final String MODULE_NAME = "carlproject";

    public static final String TABLE_PREFIX = "";

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir") + "/src/main/";
        String javaPath = projectPath + "/java";
        String resourcePath = projectPath + "/resources/mappers";

        FastAutoGenerator.create(JDBC_URL,
                        JDBC_USER,
                        JDBC_PASSWORD)
                .globalConfig(builder -> {
                    builder.author(AUTHOR)// 设置作者
                            .dateType(DateType.ONLY_DATE)
                            .disableOpenDir()
                            .enableSwagger() //生成swagger
                            .outputDir(javaPath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(PARENT_PACKAGE) // 设置父包名
                            .moduleName(MODULE_NAME) // 设置父包模块名
                            .entity("model")//设置实体类包名
                            .mapper("dao")//设置mapper包名

                            .pathInfo(Collections.singletonMap(OutputFile.xml, resourcePath)); // 设置mapperXml生成路径
                })
                .strategyConfig(
                        builder -> {
                            builder.addInclude(scanner("要生成的表名")) // 设置需要生成的表名
                                    .addTablePrefix(TABLE_PREFIX)
                                    .serviceBuilder()
                                    .formatServiceFileName("%sService")
                                    .controllerBuilder()
                                    .enableRestStyle()
                            ;


                            // 设置过滤表前缀
                        })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

    public static List<String> scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ",用英文逗号分隔：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return Arrays.asList(StringUtils.split(ipt, ","));
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }
}
